//
//  XJLRegisterViewController.m
//  XJL_WeChat
//
//  Created by XJL on 16/7/31.
//  Copyright © 2016年 Xujiali. All rights reserved.
//

#import "XJLRegisterViewController.h"
#import <ReactiveCocoa/ReactiveCocoa.h>
#import <SMS_SDK/SMSSDK.h>
#import "XJLVerifyCodeViewController.h"
#import "XJLAreaSelectedViewController.h"

@interface XJLRegisterViewController ()
@property (weak, nonatomic) IBOutlet UILabel *areaCodeLable;
@property (weak, nonatomic) IBOutlet UIButton *areaNameBtn;
@property (weak, nonatomic) IBOutlet UITextField *telField;
@property (weak, nonatomic) IBOutlet UIButton *registerBtn;

@end

@implementation XJLRegisterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    //监听文字长度的改变
    //使用ReactiveCocoa(RAC) 监听文字的改变,(思想是信号,信号被订阅)
    @weakify(self)
    [self.telField.rac_textSignal subscribeNext:^(NSString *text) {
        @strongify(self)
        self.registerBtn.enabled = text.length > 0;
    }];
    
}
- (IBAction)registerAction {
    /*
     点注册:
     1.发送短信验证码到你手机
     2.验证码发送成功,进入验证验证码的界面
     */
    //1.发送短信验证码到你的手机  模拟网络请求
//    XJLLog(@"正在发送验证码...");
    [SVProgressHUD showWithStatus:@"验证码发送中..."];
    [SMSSDK getVerificationCodeByMethod:SMSGetCodeMethodSMS phoneNumber:self.telField.text zone:self.areaCodeLable.text customIdentifier:nil result:^(NSError *error){
        //err == nil 做调试用
//        error = nil;
            if (!error) {
                [SVProgressHUD showSuccessWithStatus:@"发送验证码成功..."];
                //进入验证码界面
                XJLVerifyCodeViewController *verifyCodeVc = [[XJLVerifyCodeViewController alloc]init];
                verifyCodeVc.code = self.areaCodeLable.text;
                verifyCodeVc.tel = self.telField.text;
                [self presentViewController:verifyCodeVc animated:YES completion:nil];
                
           } else {
               [SVProgressHUD showErrorWithStatus:error.description];
           }
    }];
    
}

- (IBAction)cancle:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (IBAction)areaSelectedAction {
    //展现地区选择控制器
    XJLAreaSelectedViewController *areaSelVc = [[XJLAreaSelectedViewController alloc]init];
    //实现选中的block
    areaSelVc.areaSelectFinish = ^(NSString *name,NSString *code){
        self.areaCodeLable.text = code;
        NSString *title = [NSString stringWithFormat:@"国家/地区 %@",name];
        [self.areaNameBtn setTitle:title forState:UIControlStateNormal];
        
    };
    
    UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:areaSelVc];
    [self presentViewController:nav animated:YES completion:nil];
    
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
    [self.view endEditing:YES];
}
@end
